﻿// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
// Copyright (c) 2011, Dr. Masroor Ehsan
// All rights reserved.
// 
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions are met:
//     * Redistributions of source code must retain the above copyright
//       notice, this list of conditions and the following disclaimer.
//     * Redistributions in binary form must reproduce the above copyright
//       notice, this list of conditions and the following disclaimer in the
//       documentation and/or other materials provided with the distribution.
//     * Neither the name of the author nor the
//       names of its contributors may be used to endorse or promote products
//       derived from this software without specific prior written permission.
// 
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
// ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
// WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
// DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
// DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
// (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
// LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
// ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
// SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
// 
// $Author:$
// $Id:$
// $Rev:$
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *

using System;
using System.Collections.Generic;
using MaxTrader.Core;
using CuttingEdge.Conditions;
using Iesi.Collections.Generic;

namespace MaxTrader.BusinessObjects
{
    public class Portfolio : Entity, ICloneable
    {
        private readonly Dictionary<StockSymbol, Position> _historicalPositions;
        private readonly Dictionary<StockSymbol, Position> _openPositions;

        public Portfolio()
        {
            //_transactions = new List<ShareTransaction>();
            _openPositions = new Dictionary<StockSymbol, Position>();
            _historicalPositions = new Dictionary<StockSymbol, Position>();
            CashAccounts = new HashedSet<CashAccount>();
        }

        public virtual StockExchange StockExchange { get; set; }

        public virtual IList<AbstractTransaction> Transactions { get; protected set; }

        public virtual string PortfolioName { get; set; }

        public virtual Currency Currency { get; set; }

        public virtual ShareAccount ShareAccount { get; set; }

        public virtual Iesi.Collections.Generic.ISet<CashAccount> CashAccounts { get; protected set; }

        public virtual Dictionary<StockSymbol, Position> OpenPositions
        {
            get { return _openPositions; }
        }

        public virtual Dictionary<StockSymbol, Position> HistoricalPositions
        {
            get { return _historicalPositions; }
        }

        #region ICloneable Members

        public virtual object Clone()
        {
            //TODO: Implement
            throw new NotImplementedException();
        }

        #endregion

        public virtual void AddCashAccount(CashAccount account)
        {
            Condition.Requires(account, "account").IsNotNull();
            CashAccounts.Add(account);
        }

        public virtual void ApplyTransaction(AbstractTransaction transaction)
        {
            Condition.Requires(transaction, "transaction").IsNotNull();

            // If the transaction is older than an existing transaction then remove all
            // transactions. Put the new transaction with them, sort them all and then
            // add all of the transactions. I.e. we must add the transactions in chronological
            // order to prevent things like selling stock before we have bought it.

            //if ((Transactions.Count > 0) && Transactions[-1].CompareTo(transaction) > 0)
            //{ }
        }
    }
}